<?php
include "lib/curl.trait.php";

Class Bxcker{
	
	use curl;
	public $cookie;
	public $uid;
	
	// public function __construct( $user, $pass ){
		
	// }

	private function getCookie(){
		if( $this->cookie )
			return $this->cookie;
		$url = "http://zjxc.bxcker.com/";
		$result = $this->get( $url,false,0 )->getCookie("IEX-JSESSIONID");
		$this->cookie = $result;
		return $result;
	}
	
	function login(  $_user,$_passw,$_cookie=false ){
		if(!$_cookie){
			$_cookie = $this->getCookie();
		}
		$cookie = "IEX-JSESSIONID=" . $_cookie;
		$url = "http://zjxc.bxcker.com/qhiex_login";
		$postData = http_build_query([
			"username" => $_user,
			"password" => $_passw,
			"_spring_security_remember_me" => "false",
			"session_branchId" => ""
		]);
		// $header = ["Content-Type:application/x-www-form-urlencoded"];
		$login = $this->post( $url, $postData ,$cookie,2 )->getHeader("Location");
		if( $login =="http://zjxc.bxcker.com/login_success" ){
			return true;
		} else {
			$data = $this->lastCurl[1];
			$data = json_decode($data,true);
			return $data["message"];
		}
	}
	
	function getUid( $_cookie=false ){
		if( !$_cookie && $this->uid ){
			$uid = $this->uid;
		}
		if(!$_cookie){
			$_cookie = $this->getCookie();
		}
		$url = "http://zjxc.bxcker.com/login_success";
		$cookie = "IEX-JSESSIONID=" . $_cookie . ";";
		$uid = $this->get( $url ,$cookie,1 );
		if( !empty($uid) ){
			$data = json_decode($uid,true);
			$this->uid = $uid;
			return $data['outData']['uid'];
		}else{
			return false;
		}
	}
	
	function quickQueryCloudData( $plateNo,$_cookie=false ){
		if(!$_cookie){
			$_cookie = $this->getCookie();
		}
		$cookie = "IEX-JSESSIONID=" . $_cookie . ";";
		$url = "http://zjxc.bxcker.com/carInsure/quickQueryCloudData";
		// $postData = "plateNo={$plateNo}&vinCode=&branchId=9678&cardNo=";
		$postData = http_build_query([
			"plateNo" => $plateNo,
			"vinCode" => "",
			"branchId" => 9678,
			"cardNo" => ""
		]);
		// $header = ["Accept: application/json"];
		$data = $this->post( $url, $postData ,$cookie,1 );
		return $data;
	}
	
	function queryInsureRisk( $_cookie=false ){
		if(!$_cookie){
			$_cookie = $this->getCookie();
		}
		$cookie = "IEX-JSESSIONID=" . $_cookie . ";";
		$url = "http://zjxc.bxcker.com/carInsure/queryInsureRisk";
		$data = $this->post( $url, $postData ,$cookie,1 );
		return $data;
	}
	
	function queryInsureCompany( $_cookie=false ){
		if(!$_cookie){
			$_cookie = $this->getCookie();
		}
		$cookie = "IEX-JSESSIONID=" . $_cookie . ";";
		$url = "http://zjxc.bxcker.com/carInsure/queryInsureCompany";
		// $postData = "plateNo={$plateNo}&vinCode=&branchId=9678&cardNo=";
		$postData = http_build_query([
			"busBranchId" => 9678
		]);
		// $header = ["Accept: application/json"];
		$data = $this->post( $url, $postData ,$cookie,1 );
		return $data;
	}
}